<template>
  <div>
    <el-form
      ref="userForm"
      :model="userInfo"
      label-width="120px"
      :rules="rules"
      style="margin-left: 120px; margin-top: 30px"
    >
      <el-form-item label="姓名:" prop="username">
        <el-input v-model="userInfo.username" style="width:300px" />
      </el-form-item>
      <el-form-item label="密码:" prop="password">
        <el-input v-model="userInfo.password" type="password" style="width:300px" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="btnFn">更新</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getUserDetailById } from '@/api/user'
import { saveUserDetailById } from '@/api/employees'
export default {
  data() {
    return {
      rules: {
        username: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' }]
      },
      userInfo: {
        id: this.$route.query.id,
        username: '',
        password: ''
      }
    }
  },
  created() {
    this.loadUserInfoById()
  },
  methods: {
    async loadUserInfoById() {
      const res = await getUserDetailById(this.userInfo.id)
      console.log('获取用户详情', res)
      this.userInfo = res.data
    },
    async doSave() {
      const res = await saveUserDetailById(this.userInfo)
      console.log('保存修改结果', res)
    },
    btnFn() {
      this.$refs.userForm.validate(valid => {
        valid && this.doSave()
      })
    }
  }
}
</script>
